<?php


namespace app\sonpartner\controller;


class Members extends Common
{
    /**
     * 会员列表
     */
    public function membersList()
    {
        $id = $this -> id;
       //$id = 2;
        $input = input();
        $where = [];
        $where['u.partner_id'] = $id;
        $nickname = isset($input['nickname']) ? $input['nickname'] : '';
        if(!empty($nickname)) {
            $where['u.nickname'] = ['like', '%'.$nickname.'%'];
        }
        $code = isset($input['code']) ? $input['code'] : '';
        if(!empty($code)) {
            $where['u.code'] = ['like', '%' . $code . '%'];
        }
        $tel = isset($input['tel']) ? $input['tel'] : '';
        if(!empty($tel)) {
            $where['u.tel'] = ['like', '%' . $tel . '%'];
        }
        $tb_acc = isset($input['tb_acc']) ? $input['tb_acc'] : '';
        if(!empty($tb_acc)) {
            if($tb_acc == 1) {
                $where['u.tb_openid'] = ['NEQ', ''];
            } else if($tb_acc == 2) {
                $where['u.tb_openid'] = '';
            }
        }
        $alipay_bind = isset($input['alipay_bind']) ? $input['alipay_bind'] : '';
        if(!empty($alipay_bind)) {
            if($alipay_bind == 1) {
                $where['u.alipay_id'] = ['NEQ', ''];
            } else if($alipay_bind == 2) {
                $where['u.alipay_id'] = '';
            }
        }
        $is_vip = isset($input['is_vip']) ? $input['is_vip'] : '';
        if(!empty($is_vip)) {
            if($is_vip == 1) {
                $where['u.is_vip'] = ['NEQ', 0];
            } else {
                $where['u.is_vip'] = 0;
            }
        }
        $par_name = isset($input['par_name']) ? $input['par_name'] : '';
        if(!empty($par_name)) {
            $where['p.xingming|p.nickname'] = ['like', '%' . $par_name . '%'];
        }
        $par_code = isset($input['par_code']) ? $input['par_code'] : '';
        if(!empty($par_code)) {
            $where['p.code'] = ['like', '%' . $par_code . '%'];
        }
        $register_start = isset($input['register_start']) ? strtotime($input['register_start']) : '';
        $register_end = isset($input['register_end']) ? strtotime($input['register_end']) : '';
        if(!empty($register_start) && !empty($register_end)){
            $where['u.time'] = ['between', [$register_start, $register_end]];
        }
        $login_start = isset($input['login_start']) ? strtotime($input['login_start']) : '';
        $login_end = isset($input['login_end']) ? strtotime($input['login_end']) : '';
        if(!empty($login_start) && !empty($login_end)) {
            $where['u.last_login_time'] = ['between', [$login_start, $login_end]];
        }

        //排序字段
        $field = isset($input['field']) ? $input['field'] : 'u.id';
        $sort = isset($input['sort']) ? $input['sort'] : 'desc';
        //分页
        $limit = 20;
        $page = isset($input['page']) ? $input['page'] : 1;
        $offset = ($page - 1) * $limit;

        $data = db('user_manage')
            ->alias('u')
            ->join('user_manage p', 'u.par_id=p.id', 'LEFT')
            ->where($where)
            ->order($field . ' ' . $sort)
            ->limit($offset, $limit)
            ->field('u.id as uid,u.headimgurl,u.nickname,u.code,u.tel,u.tb_openid,u.alipay_id,u.is_vip,u.money,u.jifen,p.nickname as par_name,p.code as par_code,u.time as register_time,u.last_login_time')
            ->select();
        //淘宝有效订单
        $tb_order = db('tb_order')
            ->where('order_status', 'NEQ', 0)
            ->where('uid', 'NEQ', '')
            ->group('uid')
            ->column('uid,count(id)');
        //京东有效订单
        $jd_order = db('jd_order')
            ->where('order_status', 'NEQ', 0)
            ->where('uid', 'NEQ', '')
            ->group('uid')
            ->column('uid,count(id)');
        //拼多多有效订单
        $pdd_order = db('pdd_order')
            ->where('order_status', 'NEQ', 0)
            ->where('uid', 'NEQ', '')
            ->group('uid')
            ->column('uid,count(id)');
        //邀请好友数
        $invite_num = db('huodong_order')
            ->where('status', 1)
            ->where('par_id', 'NEQ', '')
            ->group('par_id')
            ->column('par_id,count(id)');
        //总数据条数
        $total_num = db('user_manage')
            ->alias('u')
            ->join('user_manage p', 'u.par_id=p.id', 'LEFT')
            ->where($where)
            ->count();
        //总页数
        $page_num = ceil($total_num / $limit);
        if(!empty($data)) {
            foreach ($data as $key => $value) {
                $data[$key]['last_login_time'] = date('Y-m-d H:i:s', $value['last_login_time']);
                $data[$key]['tel'] = empty($value['tel']) ? '未绑定' : $value['tel'];
                $data[$key]['tb_openid'] = empty($value['tb_openid']) ? '否' : '是';
                $data[$key]['alipay_id'] = empty($value['alipay_id']) ? '否' : '是';
                $data[$key]['is_vip'] = $value['is_vip'] == 0 ? '否' : '是';
                $tb_num = isset($tb_order[$value['uid']]) ? $tb_order[$value['uid']] : 0;
                $jd_num = isset($jd_order[$value['uid']]) ? $jd_order[$value['uid']] : 0;
                $pdd_num = isset($pdd_order[$value['uid']]) ? $pdd_order[$value['uid']] : 0;
                $data[$key]['order_num'] = $tb_num + $jd_num + $pdd_num;
                $data[$key]['invite_num'] = isset($invite_num[$value['uid']]) ? $invite_num[$value['uid']] : 0;
                $data[$key]['nickname'] = emoji_decode($data[$key]['nickname']);
                $data[$key]['par_name'] = emoji_decode($data[$key]['par_name']);
            }
            returnSuccess(['data'=>$data,'total_num'=>$total_num,'page_num'=>$page_num]);
        } else {
            returnError('暂无数据');
        }
    }

    /**
     * 邀请记录
     */
    public function inviteList()
    {
        $id = $this -> id;
//        $id = 2;
        $input = input();
        $where = [];
        $where['u.partner_id'] = $id;
        $nickname = isset($input['nickname']) ? $input['nickname'] : '';
        if(!empty($nickname)) {
            $where['h.nickname'] = ['like', '%'.$nickname.'%'];
        }
        //上级邀请人
        $par_name = isset($input['par_name']) ? $input['par_name'] : '';
        if(!empty($par_name)) {
            $where['u.nickname|u.xingming'] = ['like', '%' . $par_name . '%'];
        }
        //上级邀请码
        $par_code = isset($input['par_code']) ? $input['par_code'] : '';
        if(!empty($par_code)) {
            $where['u.code'] = ['like', '%' . $par_code . '%'];
        }
        //受邀时间
        $invite_start = isset($input['invite_start']) ? strtotime($input['invite_start']) : '';
        $invite_end = isset($input['invite_end']) ? strtotime($input['invite_end']) : '';
        if(!empty($invite_start) && !empty($invite_end)) {
            $where['h.time'] = ['between', [$invite_start, $invite_end]];
        }
        //注册时间
        $register_start = isset($input['register_start']) ? strtotime($input['register_start']) : '';
        $register_end = isset($input['register_end']) ? strtotime($input['register_end']) : '';
        if(!empty($register_start) && !empty($register_end)){
            $where['u.time'] = ['between', [$register_start, $register_end]];
        }
        //邀请状态
        $invite_status = isset($input['invite_status']) ? $input['invite_status'] : '';
        if(!empty($invite_status)) {
            if($invite_status == -1) {
                $invite_status = 0;
            }
            $where['h.status'] = $invite_status;
        }

        //排序字段
        $field = isset($input['field']) ? $input['field'] : 'h.id';
        $sort = isset($input['sort']) ? $input['sort'] : 'desc';
        //分页
        $limit = 20;
        $page = isset($input['page']) ? $input['page'] : 1;
        $offset = ($page - 1) * $limit;

        $data = db('huodong_order')
            ->alias('h')
            ->join('user_manage u', 'h.par_id=u.id', 'LEFT')
            ->where($where)
            ->order($field . ' ' . $sort)
            ->limit($offset, $limit)
            ->field('h.headimgurl,h.nickname,u.xingming,u.code,h.time as invite_time,h.status as invite_status,u.time as register_time')
            ->select();
        //总数据条数
        $total_num = db('huodong_order')
            ->alias('h')
            ->join('user_manage u', 'h.par_id=u.id', 'LEFT')
            ->where($where)
            ->count();
        //总页数
        $page_num = ceil($total_num / $limit);
        if(!empty($data)) {
            foreach ($data as $key => $value) {
                $data[$key]['invite_time'] = date('Y-m-d H:i:s', $value['invite_time']);
                switch ($value['invite_status']) {
                    case 0:
                        $data[$key]['invite_status'] = '邀请中';
                        break;
                    case 1:
                        $data[$key]['invite_status'] = '邀请成功';
                        break;
                    case 2:
                        $data[$key]['invite_status'] = '邀请超时';
                        break;
                }
            }
            returnSuccess(['data'=>$data,'total_num'=>$total_num,'page_num'=>$page_num]);
        } else {
            returnError('暂无数据');
        }
    }
}